package com.zzptc.zzsgz.bdsjws.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CheckUpdateActivity extends BaseActivity {
    @BindView(R.id.load_one)
    ImageView loadOne;
    @BindView(R.id.load_tow)
    ImageView loadTow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_check_update;
    }

    @Override
    public void initData() {
        //检查网络状态
      boolean b= checkNet();
        if (b){
            //访问服务器
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://10.0.2.2:8080/")
                    //.baseUrl("http://192.168.1.137:8080/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
          Call<VersionInfo> versionInfoCall= versionUpdate.getVersion();
            //异步处理
            versionInfoCall.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常 网络正常可以下载  网络正常已经是最新版本
                    VersionInfo versionInfo=response.body();
                    int netcode=response.body().getVersioncode();//最新的code
                    int oldcode= MyUtil.getVersionCode(MyApplication.getContext());
                    if (netcode>oldcode){
                        //setResult(3);因为要将versionInfo传过去
                        Intent intent=new Intent();
                        intent.putExtra("VersionInfo",versionInfo);
                        setResult(3,intent);
                        finish();
                    }else {
                        setResult(4);
                        finish();
                    }

                }
                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络比较慢 关闭窗体
                    setResult(2);
                    finish();
                }
            });

        }else {
            setResult(1);
            finish();
        }
    }


    private boolean checkNet() {
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }

    @Override
    public void initView() {
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(loadOne, "scaleY", 1f, 150f, 1f);
        //animatorY.setDuration(5000);
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(loadOne, "scaleX", 1f, 150f, 1f);
        final AnimatorSet set = new AnimatorSet();
        set.play(animatorX).with(animatorY);
        set.setDuration(5000);
        set.start();

        ObjectAnimator animatorY1 = ObjectAnimator.ofFloat(loadTow, "scaleY", 1f, 150f, 1f);
        //animatorY.setDuration(5000);
        ObjectAnimator animatorX1 = ObjectAnimator.ofFloat(loadTow, "scaleX", 1f, 150f, 1f);
        final AnimatorSet set1 = new AnimatorSet();
        set1.play(animatorX1).with(animatorY1);
        set1.setDuration(5000);

        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set1.start();
            }
        });
        set1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set.start();
            }
        });
    }
}
